package com.cn.sender;

import com.cn.entity.User;
import com.cn.sender.callback.ConfirmCallbackService;
import com.cn.sender.callback.ReturnCallbackService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class HelloSenderCallBack {//implements RabbitTemplate.ReturnsCallback {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private ConfirmCallbackService confirmCallbackService;
    @Autowired
    private ReturnCallbackService returnCallbackService;

    @Value("${app.rabbitmq.exchange.user}")
    private String userExchange;

    public void sendCallback() {
        User user = new User("小明-Callback","123123-Callback");
        rabbitTemplate.setReturnsCallback(returnCallbackService);
        rabbitTemplate.setConfirmCallback(confirmCallbackService);
//        rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
//            if(!ack){
//                log.info("发送消息失败:{},{}",cause,correlationData);
//            }else{
//                log.info("发送消息成功:{},{}",cause,correlationData);
//            }
//        });
        rabbitTemplate.convertAndSend("queueUser", user);
    }

    public void send() {
        User user = new User("小明","123123");
        rabbitTemplate.convertAndSend(userExchange, "user.abc", user);
    }

//    @Override
//    public void returnedMessage(ReturnedMessage returnedMessage) {
//        log.info(returnedMessage.toString());
//    }

}
